<?php
class PassportAction extends Action {
	function Login()
	{
		if(getMemberId()!=0){
			//$this->assign('jumpUrl',"/");
			$this->redirect("/");
			return ;
		}
		
		if(!$this->isPost()){
			$this->display('login');
		}else{
			$username=trim($_REQUEST['username']);
			$password=trim($_REQUEST['password']);
			
			if($user=checkMember($username, $password)){
				//登录成功
				
				if($_REQUEST['auto_login']==1)
				{
					cookie("username",$username,array('expire'	=> 3600*24*365));
					cookie("password",$password,array('expire'	=> 3600*24*365));
				}
				
				
				//print_r($user);
				setMember($user);
				
				$url=session("LASTURL");
				$this->ajaxReturn($url,"登录成功",1);
				//$this->redirect("/");
			}else{
				$this->ajaxReturn("","密码不匹配",0);
			}
		}
	}
	
	function logout()
	{
		unsetMember();
		$this->redirect("/");
	}
	
	public function reg()
	{
		if(!$this->isPost())
		{
			
			$this->display("reg");
		}else{
			$m=D("User");
			if(!isset($_REQUEST['usernmae'])){
				$_POST['username']=$_REQUEST['email'];
			}
			
			if($m->create())
			{
				$id=$m->add();
				if($id)
				{
					//注册成功
					$user=getMemberById($id);
					setMember($user);
					
					if(session("?LASTURL"))
					{
						header("Location: ",session("?LASTURL"));
					}else{
						header("Location: /");
					}
				}
				else
				{
					die($m->getDbError());
				}
			}
			else
			{
				die($m->getError());
			}
		}
	}
	
	function check()
	{
		$action=isset($_REQUEST['act'])?trim($_REQUEST['act']):'';
		switch ($action)
		{
			case 'checkname':
				$username=$_REQUEST['login'];
				$m=M("User");
				if($m->getByUsername($username))
				{
					$this->ajaxReturn(1);
				}else{
					$this->ajaxReturn(0);
				}
				break;
			case 'checkcode':
				if($_SESSION['verify'] != md5($_REQUEST['imgcode'])) {
					$this->ajaxReturn(0);
				}else{
					$this->ajaxReturn(1);
				}
			default:
				break;
		}
	}
}